﻿using System;
using System.Collections.Concurrent;

namespace Chambills.Present.Core
{
    /// <summary>
    /// Caches PropertyCaches per type
    /// </summary>
    public class TypeCache 
    {
        public readonly static TypeCache Instance;

        static TypeCache()
        {
            Instance = new TypeCache();
        }

        private readonly ConcurrentDictionary<Type, PropertyModelCache> cache = new ConcurrentDictionary<Type, PropertyModelCache>();

        public PropertyModelCache this[Type type]
        {
            get
            {
                PropertyModelCache value;
                cache.TryGetValue(type, out value);
                return value ?? cache.AddOrUpdate(type, new PropertyModelCache(type), (t,p)=>p);
            }

        }

    }
}